package com.jinyu.jyly.ui.login;

import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.base.IPresenter;
import com.jinyu.jyly.global.GlobalConstants;
import com.jinyu.jyly.ui.MainActivity;
import com.jinyu.jyly.utils.SharedPreferencesUtils;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

/**
 * 闪屏页
 */
public class SplashActivity extends BaseActivity implements MediaPlayer.OnVideoSizeChangedListener{

    @BindView(R.id.sv_start)
    SurfaceView mSurfaceView;


    @Override
    public void onBackPressed() {
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addCategory(Intent.CATEGORY_HOME);
        startActivity(i);
    }


    @Override
    public int getLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected IPresenter getPresenter() {
        return null;
    }



    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
//        setTimer();
        initVideo();
    }


    private MediaPlayer mediaPlayer;
    private void initVideo() {
        //加载视频
        mediaPlayer = MediaPlayer.create(this, Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.jyly_video));
        mediaPlayer.setOnVideoSizeChangedListener(this);

        //只有当播放器准备好了之后才能够播放，所以播放的出发只能在触发了prepare之后
//        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//            @Override
//            public void onPrepared(MediaPlayer mp) {
//                mediaPlayer.start();
//            }
//        });


        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                String token = (String) SharedPreferencesUtils.getParam(SplashActivity.this, GlobalConstants.LOGIN_TOKEN, "");
                if(!TextUtils.isEmpty(token)){
                    Intent mIntent = new Intent(SplashActivity.this, MainActivity.class);
                    startActivity(mIntent);
                }else{
                    Intent mLoinIntent = new Intent(SplashActivity.this, LoginActivity.class);
                    startActivity(mLoinIntent);
                }
                finish();
            }
        });
        //指定视频缩放模式。内容按比例缩放，并保持其纵横比。始终使用整个表面积。当内容物的纵横比与表面相同时，没有内容物被裁剪；否则，裁剪内容以适合表面。
        mediaPlayer.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);


        //得到SurfaceHolder
        SurfaceHolder holder = mSurfaceView.getHolder();
        //得到视图的状态
        holder.addCallback(new SurfaceHolder.Callback() {
            //视图销毁
            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                // TODO Auto-generated method stub

            }
            //视图创建完成
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                // TODO Auto-generated method stub
                //将寄存器设置给mediaPlayer
                mediaPlayer.setDisplay(holder);
                //异步缓冲当前视频文件，也有一个同步接口
//                mediaPlayer.prepareAsync();
                mediaPlayer.start();
            }

            //视图改变
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width,
                                       int height) {
            }
        });
    }


    private void setTimer() {
        Timer timer = new Timer();
        TimerTask tast = new TimerTask() {
            @Override
            public void run() {
                    //如果有值，说明不是第一次进来，跳转页面的话直接进登录及相关页面
                    String token = (String) SharedPreferencesUtils.getParam(SplashActivity.this, GlobalConstants.LOGIN_TOKEN, "");
                    if(!TextUtils.isEmpty(token)){
                        //说明已经登录过
                        Intent mMainIntent = new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(mMainIntent);
                        finish();
                    }else{
                        //未登录
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));//执行
                        finish();
                    }
            }
        };
        timer.schedule(tast, 5000);//10秒后
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
//        textToSpeech.stop(); // 不管是否正在朗读TTS都被打断
//        textToSpeech.shutdown(); // 关闭，释放资源
    }


    @Override
    public void onVideoSizeChanged(MediaPlayer mediaPlayer, int i, int i1) {

    }
}
